﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainProject.Quanlychiphisua
{
    public partial class Suachiphisua : Form
    {
        int idchiphi;
        public Suachiphisua()
        {
            InitializeComponent();
        }
        public Suachiphisua(int id)
        {
            InitializeComponent();
            idchiphi = id;
            loaddata();
        }

        private void loaddata()
        {
            QLXT_DBDataContext db = new QLXT_DBDataContext();
            CHIPHISUA tt = db.CHIPHISUAs.Single(p => p.IDCHIPHISUA == idchiphi);

            cmb_Idxe.DataSource = from p in db.XEs select p;
            cmb_Idxe.DisplayMember = "BIENSO";
            cmb_Idxe.ValueMember = "IDXE";
            cmb_Idxe.SelectedValue = tt.IDXE;

            cmb_Tuyensua.DataSource = from p in db.TUYENs select p;
            cmb_Tuyensua.ValueMember = "IDTUYEN";
            cmb_Tuyensua.DisplayMember = "TENTUYEN";
            cmb_Tuyensua.SelectedValue = tt.TUYEN;

            txt_Nguyennhan.Text = (tt.NGUYENNHAN).ToString();
            txt_Gara.Text = (tt.GARA).ToString();

            date_Ngaysua.Value = (DateTime)tt.NGAYSUA;
            rtxt_Ghichu.Text = tt.GHICHU;
            txt_ID.Text = (tt.IDCHIPHISUA).ToString();
            txt_Tongtien.Text = tt.TONGTIEN.ToString();
        }

        private void bnt_Sua_Click(object sender, EventArgs e)
        {

            QLXT_DBDataContext qlxt_db = new QLXT_DBDataContext();

            try
            {
                CHIPHISUA tt = qlxt_db.CHIPHISUAs.Single(p => p.IDCHIPHISUA == idchiphi);

                tt.GARA = txt_Gara.Text;
                tt.NGUYENNHAN = txt_Nguyennhan.Text;
                tt.GHICHU = rtxt_Ghichu.Text;
                tt.TONGTIEN = double.Parse(txt_Tongtien.Text);
                tt.TUYEN = (int)cmb_Tuyensua.SelectedValue;
                tt.IDXE = (int)cmb_Idxe.SelectedValue;
                tt.NGAYSUA = date_Ngaysua.Value;

                qlxt_db.SubmitChanges();
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Vui lòng nhập thông tin chính xác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void bnt_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
